#! python
# -*- coding: utf-8 -*-
"""
@File Name: 计算器_calculator_plus_emb.py
@Author: LT
@Date: 2021/10/27 14:39
# -----------------------------------------------------------------------------------------------------
@Description: 
在容器里额外添加一行按钮，用于常用操作；一个更有用的定制：通过嵌入/组合而非创建子类的方式添加按钮，用于更多操作（平方根，倒数等）；
考虑到包装pack顺序，新按钮添加到CalcGui帧之后
# -----------------------------------------------------------------------------------------------------
"""
from tkinter import *
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
__package__ = os.path.split(os.path.dirname(__file__))[-1]
from .计算器_calculator import CalcGui, getCalcArgs
from .涉及tkinter的包.组件构建器_widgets import frame, button, label


class CalcGuiPlus(Toplevel):
    def __init__(self, **args):
        Toplevel.__init__(self)
        label(self, TOP, 'PyCalc Plus - Container')
        self.calc = CalcGui(self, **args)
        frm = frame(self, BOTTOM)
        extras = [('sqrt', 'sqrt(%s)'),
                  ('x^2 ',  '(%s)**2'),
                  ('x^3 ',  '(%s)**3'),
                  ('1/x ',  '1.0/(%s)')]
        for (lab, expr) in extras:
            button(frm, LEFT, lab, (lambda expr=expr: self.onExtra(expr)))
        button(frm, LEFT, ' pi ', self.onPi)

    def onExtra(self, expr):
        text = self.calc.text
        eval = self.calc.eval
        try:
            text.set(eval.runstring(expr % text.get()))
        except:
            text.set('ERROR')

    def onPi(self):
        self.calc.text.set(self.calc.eval.runstring('pi'))


if __name__ == '__main__':
    root = Tk()
    button(root, TOP, 'Quit', root.quit)
    CalcGuiPlus(**getCalcArgs()).mainloop()       # -bg,-fg to calcgui
